/** 海康、萤石云接口 */
import store from "@/store/index.js"
const baseUrl = "https://open.ys7.com/api/";
const AppKey = "709196269f034034b20cb3924e5386a6";
const Secret = "aa2c1534807e1399112e27ea624d5c1d";

/**
 * 获取海康的视频直播地址
 * @param deviceSerial {string} 设备号
 * @param channelNo {string} 通道号
 * @param protocol {number} 播放流类型 1-ezopen、2-hls、3-rtmp、4-flv
 * @param quality {number} 视频清晰度 1-高清（主码流）、2-流畅（子码流）
 */
export const getLiveVideoUrl = (deviceSerial, channelNo = 1, protocol = 2, quality = 2) => {
    return new Promise(resolve => {
		console.log( store.getters.getEzuikitAccessToken, "token");
		uni.request({
			url: `${baseUrl}lapp/v2/live/address/get?accessToken=${store.getters.getEzuikitAccessToken}&deviceSerial=${deviceSerial}&channelNo=${channelNo}&protocol=${protocol}&quality=${quality}`,
			method: "POST",
			header: {
			    "Content-Type": "application/x-www-form-urlencoded",
			},
			success: res => {
				console.log(res, "res");
				if(res && res.data) {
					const { data } = res.data;
					console.log(data, "");
					if(data && data.url) {
						resolve(data.url);
						return;
					}
				}
				resolve(null);
			}
		})
    });
}


/** 获取accessToken */
export const getAccessToken = () => {
    return new Promise(resolve => {
        uni.request({
			url: baseUrl + `lapp/token/get?appKey=${AppKey}&appSecret=${Secret}`,
			method: "POST",
			success: res => {
				if(res && res.data) {
					const { data } = res.data;
					if(data) {
						const { accessToken } = data;
						store.commit("setEzuikitAccessToken", accessToken);
					}
				}
				resolve();
			}
		})
    });
}